﻿function EducationViewModel(parent) {
    var self = this;

    // knockout variables
    self.educations = ko.observableArray([]);
    self.workingEducation = ko.observable(null);

    //PAGING + SEARCH
    self.search = ko.observable("");
    self.currentPage = ko.observable(0);
    self.pageSize = ko.observable(10);
    self.pagesCount = ko.observable(1);
    self.count = ko.observable(0);
    self.lastPage = function () {
        self.currentPage(self.pagesCount() - 1);
    };
    self.firstPage = function () {
        self.currentPage(0);
    };
    self.nextPage = function () {
        if (self.currentPage() < self.pagesCount() - 1) {
            self.currentPage(self.currentPage() + 1);
        };
    };
    self.previousPage = function () {
        if (self.currentPage() > 0) {
            self.currentPage(self.currentPage() - 1);
        }
    };
    
    //SELECT EDUCATION
    self.selectEducation = function(education) {
        self.workingEducation(education);
    };
    self.deselectEducation = function() {
        self.workingEducation(null);
    };
    self.select = function(education) {
        self.selectEducation(education);
    };
    self.selectNew = function () {
        var newEducation = new Education({});
        self.educations.push(newEducation);
        self.selectEducation(newEducation);
    };
    
    //CRUD OPERATIONS
    self.saveNewEducation = function () {
        if (self.workingEducation().errors().length == 0) {
            $.ajax({
                    url: "/Education/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingEducation()),
                    contentType: 'application/json',
                    success: function(id) {
                        self.workingEducation().Id(id);
                        self.deselectEducation();
                        $("#CreateEducation").modal("hide");
                    }
                });
        }
    };
    self.saveEditEducation = function () {
        if (self.workingEducation().errors().length == 0) {
            $.ajax({
                    url: "/Education/Save",
                    type: 'post',
                    data: ko.toJSON(self.workingEducation()),
                    contentType: 'application/json',
                    success: function() {
                        self.deselectEducation();
                        $("#EditEducation").modal("hide");
                    }
                });
        }
    };
    self.DeleteEducation = function () {
        $.post("/Education/DeleteEducation", { id : self.workingEducation().Id() }, function () {
            self.educations.pop(self.workingEducation());
            self.deselectEducation();
        });
        $('#DeleteEducation').modal('hide');
    };
    self.cancelNewEducation = function () {
        self.educations.pop(self.workingEducation);
        self.deselectEducation();
        $('#CreateEducation').modal('hide');
    };
    self.cancelEditEducation = function () {
        self.loadEducations();
        self.deselectEducation();
        $('#EditEducation').modal('hide');
    };
    self.cancelDetails= function () {
        self.deselectEducation();
        $('#DetailsEducation').modal('hide');
    };
    self.cancelDeleteEducation = function () {
        self.deselectEducation();
        $('#DeleteEducation').modal('hide');
    };
    self.loadEducations = function (pageIndex, pageSize, search) {
        $.getJSON("/Education/List", { search: search, pageIndex: pageIndex, pageSize: pageSize }, function (allData) {
            self.pagesCount(allData.PagesCount);
            self.count(allData.Count);
            var mappedEducations = $.map(allData.Entities, function (item) { return new Education(item); });
            self.educations(mappedEducations);
        });
    };
    self.triggerLoadEducations = ko.computed(function () {
        self.currentPage();
        self.pageSize();
        var search = self.search();
        if (parent.route() == "education") {
            var route = '#/Education/List/?pageIndex=' + self.currentPage() + '&pageSize=' + self.pageSize();
            if (search != null && search != "") {
                route = route + '&search=' + self.search();
            }
            location.hash = route;
        }
    }, this);

}